package com.zh.opcua.milo;

import com.google.common.collect.ImmutableList;
import lombok.SneakyThrows;
import org.bouncycastle.asn1.eac.UnsignedInteger;
import org.eclipse.milo.opcua.sdk.client.OpcUaClient;
import org.eclipse.milo.opcua.sdk.client.api.UaClient;
import org.eclipse.milo.opcua.sdk.client.model.nodes.variables.BaseDataVariableNode;
import org.eclipse.milo.opcua.stack.core.types.builtin.DataValue;
import org.eclipse.milo.opcua.stack.core.types.builtin.NodeId;
import org.eclipse.milo.opcua.stack.core.types.builtin.StatusCode;
import org.eclipse.milo.opcua.stack.core.types.builtin.Variant;
import org.eclipse.milo.opcua.stack.core.types.builtin.unsigned.UInteger;
import org.eclipse.milo.opcua.stack.core.types.builtin.unsigned.UShort;
import org.eclipse.milo.opcua.stack.core.types.builtin.unsigned.Unsigned;
import org.eclipse.milo.opcua.stack.core.types.enumerated.TimestampsToReturn;
import org.junit.jupiter.api.Test;

import java.util.List;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;

/**
 * Copyright (C) 广州中和互联网技术有限公司
 *
 * @author 范家微 fanjiawei@borche.cn
 * @date :2020/12/29 16:08
 */
public class opcWrite {


	/**
	 * 写数据成功
	 * @throws Exception
	 * @throws InterruptedException
	 */
	@Test
	public void test66() throws Exception, InterruptedException {


		OpcUaClient client = OPCUAClientUtils.createOpcUaClient();
		client.connect().get();
		//for (int i = 50345; i < 51000; i++) {
		NodeId nodeId = new NodeId(1, 50239);
		Object value = client.readValue(0.0, TimestampsToReturn.Both, nodeId).get().getValue().getValue();
		System.out.println("读出来的值" + value);

		Variant variant = new Variant(UInteger.valueOf(888888));
		DataValue dataValue = new DataValue(variant);

		NodeId nodeId2 = new NodeId(1, 50240);
		Variant variant2 = new Variant(UShort.valueOf(46578));

		DataValue dataValue2 = new DataValue(variant2);
		boolean good = client.writeValue(nodeId, dataValue).get().isGood();
		boolean good2 = client.writeValue(nodeId2, dataValue2).get().isGood();
		System.out.println(good);
		System.out.println(good2);
		//}

	}


	@Test
	public void test() throws Exception {
		OpcUaClient client = OPCUAClientUtils.createOpcUaClient();
		client.connect().get();
		//NodeId nodeId = new NodeId(2, "通道 2.设备 1.TAG5");
		NodeId nodeId = new NodeId(1, 50239);
		// 先读取
		CompletableFuture<DataValue> future = client.readValue(0.0, TimestampsToReturn.Both, nodeId);
		System.out.println("写入前" + future.get().getValue().getValue());
		// 在写入
		long l = 12345L;
		new BaseDataVariableNode(client, nodeId).setValue(l).get();
//		DataValue dataValue = new DataValue(new Variant(),null,null);
//		CompletableFuture<StatusCode> future2 = client.writeValue(nodeId, dataValue);
//		future2.exceptionally((e) -> {
//			e.printStackTrace();
//			future2.completeExceptionally(e);
//			return null;
//		});
//		future2.thenAccept(result->{
//			System.out.println(result.isGood());
//		});
		//System.out.println(statusCode.isGood());
		Thread.sleep(200);
		//再读取
		CompletableFuture<DataValue> future1 = client.readValue(0.0, TimestampsToReturn.Both, nodeId);
		System.out.println("写入后" + future1.join().getValue().getValue());


	}


	@Test
	public void test2() throws ExecutionException, InterruptedException {
		CompletableFuture<String> completableFuture = new CompletableFuture<>();
		new Thread(() -> {
			// 模拟执行耗时任务
			System.out.println("task doing...");
			try {
				Thread.sleep(3000);
				int i = 1 / 0;
			} catch (Exception e) {
				// 告诉completableFuture任务发生异常了
				completableFuture.completeExceptionally(e);
			}
			// 告诉completableFuture任务已经完成
			completableFuture.complete("ok");
		}).start();
		// 获取任务结果，如果没有完成或出异常会一直阻塞等待
		String result = completableFuture.get();
		System.out.println("计算结果:" + result);
	}


}
